// var wang = require("/utils/Convert_Pinyin.js") 
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    array: ['男', '女'],
    index: 0,
    placeWords: "请输入您的",

    basicNewsList: [{
        til: "姓名",
        detail: "",
      },
      {
        til: "性别",
        detail: "",
      },
      {
        til: "出生日期",
        detail: "",
      },
      {
        til: "身份证号",
        detail: "",
      },
    ],
    newAddBasicshow: false,

  },
  basicNews: function(e) {
    let that = this
    let basicNewsList = that.data.basicNewsList
    let basicName = e.detail.value
    let uid = wx.getStorageSync("userInfo")
    // 把til给变成一个字符串
    let tilArr = []
    // 把title变成一个字符串
    let detailArr = []
    for (let i = 0; i < basicNewsList.length; i++) {
      basicNewsList[i].detail = basicName[i]
      tilArr.push(basicNewsList[i].til)
      detailArr.push(basicNewsList[i].detail)
    }
    console.log(tilArr.toString() + "<================>" + detailArr)
    var url = app.util.url('entry/wxapp/dasic')
    app.util.request({
      url: url,
      data: {
        uid: uid,
        m: 'md_daojia',
        op: "create",
        tilArr: tilArr.toString(),
        detailArr: detailArr.toString()
      },
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel: true,
          success: function(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/index/index?currentTab=' + 1,
              })
            }
          }
        })
      }
    })
  },
  addBasicBtn: function(e) {
    let that = this
    that.setData({
      newAddBasicshow: !that.data.newAddBasicshow
    })
  },
  // 新加 增加的基本信息条目
  addNewBasicList: function(e) {
    console.log(e.detail.value)
    let addTilBasic = e.detail.value.addTilBasic
    let addWordBasic = e.detail.value.addWordBasic
    let that = this
    let newAddBasic = that.data.basicNewsList
    if (addTilBasic == "") {
      wx.showModal({
        title: '提示',
        content: '标题不能为空，请重新填写！',
        showCancel: true,
        success: function(res) {},
      })
      return
    }
    newAddBasic.push({
      til: addTilBasic,
      detail: addWordBasic
    })
    that.setData({
      basicNewsList: newAddBasic,
      newAddBasicshow: !that.data.newAddBasicshow
    })
  },
  closeInforMationPop:function(){
    let that = this
    that.setData({
      newAddBasicshow: !that.data.newAddBasicshow
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this

    let uid = wx.getStorageSync("userInfo")
    var url = app.util.url('entry/wxapp/dasic')
    app.util.request({
      url: url,
      data: {
        m: 'md_daojia',
        op: "display",
        uid: uid
      },
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        console.log(res)
        let tilArr = res.data.data.tilArr.split(',')
        let detailArr = res.data.data.detailArr.split(',')
        for (let i = 0; i < tilArr.length; i++) {
          that.data.basicNewsList[i] = {
            til: tilArr[i],
            detail: detailArr[i]
          }
        }
        that.setData({
          basicNewsList: that.data.basicNewsList
        })
      }
    })
  },

  // 性别选择
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  // 出身时间选择
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  // 表单保存
  formInformation: function(t) {
    var values = t.detail.value
    var that = this;
    let uname = values.uname //姓名
    let idcard = values.idcard //身份证号
    let sbnumber = values.sbnumber //社保号
    let faddress = values.faddress //家庭住址
    let heigh = values.heigh //身高
    let weight = values.weight //体重
    let allergy = values.allergy //过敏
    let notice = values.notice //健康注意事项
    console.log("values", values)
    if (uname == "" || uname == undefined) {
      wx.showToast({
        title: '请输入姓名',
        icon: "none"
      })
    } else if (!(/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(idcard))) {
      wx.showToast({
        title: '身份证号格式错误',
        icon: "none"
      })
    } else if (sbnumber == undefined || sbnumber == "") {
      wx.showToast({
        title: '社保账号为空',
        icon: "none"
      })
    } else if (faddress == undefined || faddress == "") {
      wx.showToast({
        title: '家庭住址为空',
        icon: "none"
      })
    } else if (heigh == undefined || heigh == "") {
      wx.showToast({
        title: '身高为空',
        icon: "none"
      })
    } else if (weight == undefined || weight == "") {
      wx.showToast({
        title: '体重为空',
        icon: "none"
      })
    } else if (allergy == undefined || allergy == "") {
      wx.showToast({
        title: '过敏为空',
        icon: "none"
      })
    } else if (notice == undefined || notice == "") {
      wx.showToast({
        title: '注意事项为空',
        icon: "none"
      })
    } else {

      //基本信息请求接口
      var url = app.util.url('entry/wxapp/dasic')
      app.util.request({
        url: url,
        data: {
          m: 'md_daojia',
          op: 'create',
          uid: wx.getStorageSync('userInfo'),
          idnum: idcard,
          social: sbnumber,
          addres: faddress,
          name: uname,
          sex: that.data.index,
          height: heigh,
          weight: weight,
          time: that.data.date,
          allergy: allergy,
          health: notice
        },
        success: function(res) {
          var list = res.data.data

          that.setData({
            // announCement: list
          })
          wx.showModal({
            title: '提示',
            content: res.message,
            showCancel: true,
            success: function(res) {
              console.log(res.confirm)
              if (res.confirm == true) {
                wx.reLaunch({
                  url: '/pages/index/index?currentTab=' + 1,
                })
              }
            },
          })


          console.log("基本详细表单提交=========>", res);
        }
      })

    }
  },
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})